package activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.hichip.campro.R;

import java.io.File;
import java.util.ArrayList;

import base.HiActivity;
import bean.MyCamera;
import bean.PhotoListItem;
import butterknife.BindView;
import common.HiDataValue;
import common.TitleView;
import main.adapter.PhotoListAdapter;
import utils.HiToast;

/**
 * 侧滑栏里面 照片集合窗口
 * Created by lt on 2018\2\23 0023.
 */

public class PhotoListActivity extends HiActivity implements AdapterView.OnItemClickListener {
    @BindView(R.id.title) TitleView titleView;
    @BindView(R.id.gridview) GridView gridview;
    private ArrayList<PhotoListItem> items = new ArrayList<>();


    @Override
    protected int setLayoutId() {
        return R.layout.activity_photo_list;
    }

    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        initViewAndData();
        setListeners();

    }

    @Override
    protected void onResume() {
        super.onResume();
        //数据源
        items.clear();
        readyData();
        //if(items.size()>0){
        gridview.setAdapter(new PhotoListAdapter(this, items));
        // }
    }

    private void setListeners() {
        gridview.setOnItemClickListener(this);
    }

    private void initViewAndData() {
        titleView.setButton(TitleView.NAVIGATION_BUTTON_LEFT);
        titleView.setTitle("照片集合");
        titleView.setNavigationBarButtonListener(new TitleView.NavigationBarButtonListener() {
            @Override
            public void OnNavigationButtonClick(int which) {
                switch (which) {
                    case TitleView.NAVIGATION_BUTTON_LEFT:
                        PhotoListActivity.this.finish();
                        break;
                }
            }
        });
//        //数据源
//        readyData();
//        if(items.size()>0){
//            gridview.setAdapter(new PhotoListAdapter(this,items));
//        }
    }

    private void readyData() {
        for (MyCamera camera : HiDataValue.CameraList) {
            int num = getFileCount(camera.getUid());
            if (num > 0) {
                PhotoListItem item = new PhotoListItem(num, camera.getNikeName(), getPath(camera.getUid()), camera.getUid());
                items.add(item);
            }
        }
    }


    // 获取图片数量，
    public int getFileCount(String uid) {
        String path = HiDataValue.getPathCameraSnapshot(uid,this);
        File folder = new File(path);
        String[] imageFiles = folder.list();
        if (imageFiles == null) {
            return 0;
        }
        int sum = 0;
        sum = imageFiles.length;
        return sum;
    }

    //获取一个缩略图的路径
    public String getPath(String uid) {
        String path =HiDataValue.getPathCameraSnapshot(uid,this);
        File folder = new File(path);
        String[] imageFiles = folder.list();
        if (imageFiles == null) {
            return null;
        }
        return path + imageFiles[0];
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (position < 0) return;
        PhotoListItem item = items.get(position);
        if (getFileCount(item.getUid()) == 0) {
            HiToast.showToast(this, getString(R.string.tips_no_picture));
            return;
        }
        Bundle extras = new Bundle();
        extras.putString(HiDataValue.EXTRAS_KEY_UID, item.getUid());
        Intent intent = new Intent();
        intent.putExtras(extras);
        intent.setClass(this, LocalPictureActivity.class);
        startActivity(intent);

    }

}






